package com.frxuexi.video

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.SearchView.OnQueryTextListener
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.adapter.VideoAdapter
import com.frxuexi.lib_base.entity.SearchBean
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.video.databinding.ActivitySearchBinding
import dagger.hilt.android.AndroidEntryPoint
import org.w3c.dom.Text

@Route(path = "/video/SearchActivity")
@AndroidEntryPoint
class SearchActivity : BaseActivity<ActivitySearchBinding,VideoViewModel>() {
    val videoAdapter by lazy { VideoAdapter() }
    var list= mutableListOf<SearchBean>()
    override fun initData() {
        vm.searchSuc.observe(this){
            Log.i("===",it.toString())
            videoAdapter.data.clear()
            videoAdapter.addData(it)
            videoAdapter.notifyDataSetChanged()
        }

        vm.addSearchSuc.observe(this){
            Log.i("===",it.toString())
            vm.querySearch()
        }

        vm.querySearchSuc.observe(this){
            list.addAll(it)
            Log.i("===",it.toString())
            binding.videoFlex.removeAllViews()
            it.forEach {bean->
                val view =
                    LayoutInflater.from(this@SearchActivity).inflate(R.layout.flex_search, null)
                view.findViewById<TextView>(R.id.flex_title).apply {
                    setText(bean.key)
                    setOnClickListener {
                        binding.videoSearch.setQuery(bean.key,true)

                    }
                }

                view.findViewById<ImageView>(R.id.flex_delete).setOnClickListener {
                    vm.deleteSearch(bean)
                    binding.videoFlex.removeView(view)
                }

                binding.videoFlex.addView(view)
            }
        }
    }

    override fun initView() {
        binding.videoSearch.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                Log.i("===",query!!)
                vm.searchVideos(query!!)
                vm.addSearch(SearchBean(key = query!!))
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }

        })

        vm.querySearch()

        binding.rvSearch.apply {
            adapter=videoAdapter
            layoutManager=StaggeredGridLayoutManager(2,RecyclerView.VERTICAL)
        }

        binding.videoClear.setOnClickListener {
            binding.videoFlex.removeAllViews()
            list.forEach {
                vm.deleteSearch(it)
            }
        }


    }

}